package com.tensquare.friend.controller;

import com.tensquare.friend.client.UserClient;
import com.tensquare.friend.server.FriendService;
import com.tensquare.friend.server.NoFriendService;
import dto.ResultDTO;
import dto.StatusCode;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/friend")
public class FriendController {
    @Autowired
    private FriendService friendService;

    @Autowired
    private NoFriendService noFriendService;

    @Autowired
    private HttpServletRequest httpServletRequest;

    @Autowired
    private UserClient userClient;

    /**
     * 喜欢好友或不喜欢好友
     *
     * @param friendid
     * @param type     1: 喜欢， 0： 不喜欢
     * @return
     */
    @RequestMapping(value = "/like/{friendid}/{type}", method = RequestMethod.PUT)
    public ResultDTO addFriend(@PathVariable(name = "friendid") String friendid,
                               @PathVariable(name = "type") String type) {
        // 校验权限，至少登录 ,用户为user
        Claims user_claims = (Claims) this.httpServletRequest.getAttribute("user_claims");

        if (user_claims == null) {
            // 未登录
            return new ResultDTO(false, StatusCode.LOGINERROR, "权限不足");
        }

        // 添加好友
        // 判断喜欢还是不喜欢
        if ("1".equals(type)) {
            // 标注是否成功添加好友
            int flag = this.friendService.addFriend(user_claims.getId(), friendid);
            // 喜欢
            if (flag == 0) {
                return new ResultDTO(false, StatusCode.REPERROR, "重复喜欢好友了");
            }
            if (flag == 1) {
                // 成功添加好友
                // 远程调用user微服务，添加关注数和粉丝数
                this.userClient.updateFansCountAndFollowCount(user_claims.getId(), friendid, 1);
                return new ResultDTO(true, StatusCode.OK, "添加成功");
            }
        } else if ("0".equals(type)) {
            // 不喜欢
            // 向不喜欢的数据表添加数据
            // 标志是否成功添加非好友
            int flag = this.noFriendService.addNoFriend(user_claims.getId(), friendid);
            if (flag == 0) {
                // 重复删除了
                return new ResultDTO(false, StatusCode.REPERROR, "重复不喜欢好友了");
            }
            if (flag == 1) {
                // 成功删除
                // 远程调用user微服务，把自己的关注数-1，对方的粉丝数-1
                this.userClient.updateFansCountAndFollowCount(user_claims.getId(), friendid, -1);
                return new ResultDTO(true, StatusCode.OK, "不喜欢成功");
            }
        }
        return new ResultDTO(true, StatusCode.OK, "操作成功");
    }


    /**
     * 删除好友
     *
     * @param friendid
     * @return
     */
    @RequestMapping(value = "/{friendid}", method = RequestMethod.DELETE)
    public ResultDTO deleteFriend(@PathVariable(name = "friendid") String friendid) {
        Claims user_claims = (Claims) this.httpServletRequest.getAttribute("user_claims");
        if (user_claims == null) {
            // 未登录
            return new ResultDTO(false, StatusCode.ACCESSERROR, "权限不足");
        }
        this.friendService.deleteFriend(user_claims.getId(), friendid);
        // 远程调用user微服务，把自己的关注数-1，对方的粉丝数-1
        this.userClient.updateFansCountAndFollowCount(user_claims.getId(), friendid, -1);

        return new ResultDTO(true, StatusCode.OK, "删除成功");
    }
}
